package com.example.city_defense;

import com.badlogic.androidgames.framework.Pixmap;
import com.badlogic.androidgames.framework.Sound;

public class Weapon {

	public String name;
	public int x, y;
	public int R;
	public int timeRecharge;
	public int cost;
	
	public Pixmap pixmap;
	public Sound sound;
	
	public int TYPE;
	public boolean isLock;
	public boolean isRecharge;
	public boolean boom;
	
	public Bullet bullet;
	
	public Weapon(int x, int y, int TYPE) {
		// TODO Auto-generated constructor stub
		this.x = x*50;
		this.y = y*50+75;
		this.TYPE = TYPE;
		
		if(TYPE == 1){
			this.R = 3*50;
			this.timeRecharge = 10; //s
			this.cost = 20 * TYPE;
			this.pixmap = Asset.basic1;
			this.sound = Asset.boom;
		}
		else if(TYPE == 2){
			this.R = 3*50;
			this.timeRecharge = (11 - TYPE)*100; //ms
			this.cost = 20 * TYPE;
			this.pixmap = Asset.basic1;
			this.sound = Asset.boom;
		}
		else if(TYPE == 3){
			this.R = 3*50;
			this.timeRecharge = (11 - TYPE)*100; //ms
			this.cost = 20 * TYPE;
			this.pixmap = Asset.basic1;
			this.sound = Asset.boom;
		}
		else if(TYPE == 4){
			this.R = 3*50;
			this.timeRecharge = (11 - TYPE)*100; //ms
			this.cost = 20 * TYPE;
			this.pixmap = Asset.basic1;
			this.sound = Asset.boom;
		}
		else if(TYPE == 5){
			this.R = 3*50;
			this.timeRecharge = (11 - TYPE)*100; //ms
			this.cost = 20 * TYPE;
			this.pixmap = Asset.basic1;
			this.sound = Asset.boom;
		}
		else if(TYPE == 6){
			this.R = 3*50;
			this.timeRecharge = (11 - TYPE)*100; //ms
			this.cost = 20 * TYPE;
			this.pixmap = Asset.extend1;
			this.sound = Asset.boom;
		}
		else if(TYPE == 7){
			this.R = 3*50;
			this.timeRecharge = (11 - TYPE)*100; //ms
			this.cost = 20 * TYPE;
			this.pixmap = Asset.extend1;
			this.sound = Asset.boom;
		}
		else if(TYPE == 8){
			this.R = 3*50;
			this.timeRecharge = (11 - TYPE)*100; //ms
			this.cost = 20 * TYPE;
			this.pixmap = Asset.extend1;
			this.sound = Asset.boom;
		}
		else if(TYPE == 9){
			this.R = 3*50;
			this.timeRecharge = (11 - TYPE)*100; //ms
			this.cost = 20 * TYPE;
			this.pixmap = Asset.extend1;
			this.sound = Asset.boom;
		}
		else if(TYPE == 10){
			this.R = 3*50;
			this.timeRecharge = (11 - TYPE)*100; //ms
			this.cost = 20 * TYPE;
			this.pixmap = Asset.extend1;
			this.sound = Asset.boom;
		}
		else if(TYPE == 11){
			this.R = 3*50;
			this.timeRecharge = (11 - TYPE)*100; //ms
			this.cost = 20 * TYPE;
			this.pixmap = Asset.ability1;
			this.sound = Asset.boom;
		}
		else if(TYPE == 12){
			this.R = 3*50;
			this.timeRecharge = (11 - TYPE)*100; //ms
			this.cost = 20 * TYPE;
			this.pixmap = Asset.ability1;
			this.sound = Asset.boom;
		}
		else if(TYPE == 13){
			this.R = 3*50;
			this.timeRecharge = (11 - TYPE)*100; //ms
			this.cost = 20 * TYPE;
			this.pixmap = Asset.ability1;
			this.sound = Asset.boom;
		}
		else if(TYPE == 14){
			this.R = 3*50;
			this.timeRecharge = (11 - TYPE)*100; //ms
			this.cost = 20 * TYPE;
			this.pixmap = Asset.ability1;
			this.sound = Asset.boom;
		}
		else if(TYPE == 15){
			this.R = 3*50;
			this.timeRecharge = (11 - TYPE)*100; //ms
			this.cost = 20 * TYPE;
			this.pixmap = Asset.ability1;
			this.sound = Asset.boom;
		}
		
		this.isLock = false;
		this.isRecharge = false;
		bullet = new Bullet(x, y, 2);
		bullet.isAlive = false;
	}
	
	public void fire(Enemy enemy) {
		float x1 = Math.abs(enemy.x-this.x);
		float y1 = Math.abs(enemy.y-this.y);
		float d1 = (float)Math.sqrt(Math.pow(x1, 2)+ Math.pow(y1, 2));
		
		if(d1<=R){
			bullet.isAlive = true;
			double alpha = Math.atan(x1/y1);
			int Vx= Math.abs((int)(bullet.speed*Math.sin(alpha)));
			int Vy= Math.abs((int)(bullet.speed*Math.cos(alpha)));
			if(enemy.x < bullet.x) Vx = -Vx;
			if(enemy.y < bullet.y) Vy = -Vy;
			
			bullet.setVelocity(Vx, Vy);
			bullet.move();
			isRecharge = true;
		}
		
		float x2 = Math.abs(enemy.x-bullet.x);
		float y2 = Math.abs(enemy.y-bullet.y);
		float d2 = (float)Math.sqrt(Math.pow(x2, 2)+ Math.pow(y2, 2));
		
		if(d2<=50){
			destroyEnemy(enemy);
		}
		
	}
	
	public void destroyEnemy(Enemy enemy) {
		bullet.isAlive = false;
		bullet.x = this.x;
		bullet.y = this.y;
		enemy.blood -= bullet.damage;
		enemy.damage += bullet.damage;	
		if(enemy.blood == 0)
		{
			enemy.isAlive = false;
			boom = true;
		}	
	}
}